#include "../include/tcp_client_ctl.h"

void recv_file(int socket_fd)
{
    int ret = -1;
    char file_name[1024];
    bzero(file_name, 1024);
    //第一步：接收文件名
    ret = recv(socket_fd, file_name, sizeof(file_name), 0);
    if(ret < 0)
    {
        perror("file_name recv failed");
        exit(1);
    }
    if(strcmp(file_name, END) == 0)
    {
        printf("文件传输已取消\n");
        return;
    }

    printf("文件名称：%s\n", file_name);

    int file_size = 0;
    ret = recv(socket_fd, &file_size, sizeof(file_size), 0);
    if(ret < 0)
    {
        perror("file_name recv failed");
        exit(1);
    }
    printf("文件大小：%d\n", file_size);

    int fd_file = open(file_name, O_RDWR | O_CREAT | O_TRUNC);
    if(fd_file < 0)
    {
        perror("fd_file open failed");
        exit(1);
    } 

    int write_ret = 0;
    char buf[1024];

    int size = file_size;
    int total_size = 0;

    while(size > 0)
    {
        memset(buf, 0, sizeof(buf));
        ret = recv(socket_fd, buf, sizeof(buf), 0);
        if(ret == 0)
        {
            break;
        }
        
        if(ret < 0)
        {
            perror("recv failed");
            exit(1);
        }

        write_ret = write(fd_file, buf, ret);
        if(write_ret < 0)
        {
            perror("write failed");
            exit(1);
        }
        else if(write_ret == 0)
        {
            break;
        }

        total_size += ret;
        size -= ret;
        printf("ret = %d\n", write_ret);
    }

    if(file_size == total_size)
        printf("文件接收成功\n");
    else
        printf("文件接收失败\n");
}

void* display1(void *arg)
{
    pthread_detach(pthread_self());
    while(1)
    {
        lcd_draw_jpg(0, 0, "3-1.jpg", NULL, 0, 0);
        sleep(2);
        lcd_draw_jpg(0, 0, "3-2.jpg", NULL, 0, 0);
        sleep(2);
    }
    pthread_exit(NULL);
}

void* display2(void *arg)
{
    pthread_detach(pthread_self());
    while(1)
    {
        lcd_draw_jpg(0, 0, "3-1.jpg", NULL, 0, 0);
        sleep(2);
        lcd_draw_jpg(0, 0, "3-3.jpg", NULL, 0, 0);
        sleep(2);
    }
    pthread_exit(NULL);
}

void* display3(void *arg)
{
    pthread_detach(pthread_self());
    while(1)
    {
        lcd_draw_jpg(0, 0, "3-2.jpg", NULL, 0, 0);
        sleep(2);
        lcd_draw_jpg(0, 0, "3-3.jpg", NULL, 0, 0);
        sleep(2);
    }
    pthread_exit(NULL);
}

void* display4(void *arg)
{
    pthread_detach(pthread_self());
    while(1)
    {
        lcd_draw_jpg(0, 0, "3-1.jpg", NULL, 0, 0);
        sleep(2);
        lcd_draw_jpg(0, 0, "3-2.jpg", NULL, 0, 0);
        sleep(2);
        lcd_draw_jpg(0, 0, "3-3.jpg", NULL, 0, 0);
        sleep(2);
    }
    pthread_exit(NULL);
}

void disp_ad(int socket_fd)
{
    int ret = -1;
    char cmd[1024];
    bzero(cmd, 1024);
    int status_arr[4];
    for(int i = 0; i < 4; i ++)
    {
        status_arr[i] = 0;
    }
        
    pthread_t tid[4];

    while(1)
    {
        ret = recv(socket_fd, cmd, sizeof(cmd), 0);
        printf("cmd: %s\n", cmd);
        if(ret < 0)
        {
            perror("cmd recv failed");
            exit(1);
        }
        else if(ret == 0)
        {
            printf("服务器掉线\n");
            return;
        }
        if(strcmp(cmd, "000") == 0)
        {
            int i = 0;
            for(i = 0; i < 4; i ++)
            {
                if(status_arr[i] == 1)
                {
                    pthread_cancel(tid[i]);
                    status_arr[i] = 0;
                }
            }
            lcd_draw_jpg(0, 0, "3-4.jpg", NULL, 0, 0);
            printf("广告播放已取消\n");
            return;
        }

        else if(strcmp(cmd, "001") == 0)
        {
            int i = 0;
            for(i = 0; i < 4; i ++)
            {
                if(status_arr[i] == 1)
                {
                    pthread_cancel(tid[i]);
                    status_arr[i] = 0;
                }
            }
            lcd_draw_jpg(0, 0, "3-1.jpg", NULL, 0, 0);
            printf("凯迪拉克广告播放成功\n");
        }

        else if(strcmp(cmd, "010") == 0)
        {
            int i = 0;
            for(i = 0; i < 4; i ++)
            {
                if(status_arr[i] == 1)
                {
                    pthread_cancel(tid[i]);
                    status_arr[i] = 0;
                }
            }
            lcd_draw_jpg(0, 0, "3-2.jpg", NULL, 0, 0);
            printf("奥迪Q7广告播放成功\n");
        }

        else if(strcmp(cmd, "100") == 0)
        {
            int i = 0;
            for(i = 0; i < 4; i ++)
            {
                if(status_arr[i] == 1)
                {
                    pthread_cancel(tid[i]);
                    status_arr[i] = 0;
                }
            }
            lcd_draw_jpg(0, 0, "3-3.jpg", NULL, 0, 0);
            printf("迈腾广告播放成功\n");
        }

        ////////////////////////////
        //轮播多张图片
        else if(strcmp(cmd, "011") == 0)
        {
            int i = 0;
            for(i = 0; i < 4; i ++)
            {
                if(status_arr[i] == 1)
                {
                    pthread_cancel(tid[i]);
                    status_arr[i] = 0;
                }
            }
            pthread_create(&tid[0], NULL, display1, NULL);
            status_arr[0] = 1;
        }

        else if(strcmp(cmd, "101") == 0)
        {
            int i = 0;
            for(i = 0; i < 4; i ++)
            {
                if(status_arr[i] == 1)
                {
                    pthread_cancel(tid[i]);
                    status_arr[i] = 0;
                }
            }
            pthread_create(&tid[1], NULL, display2, NULL);
            status_arr[1] = 1;
        }

        else if(strcmp(cmd, "110") == 0)
        {
            int i = 0;
            for(i = 0; i < 4; i ++)
            {
                if(status_arr[i] == 1)
                {
                    pthread_cancel(tid[i]);
                    status_arr[i] = 0;
                }
            }
            pthread_create(&tid[2], NULL, display3, NULL);
            status_arr[2] = 1;
        }

        else if(strcmp(cmd, "111") == 0)
        {
            int i = 0;
            for(i = 0; i < 4; i ++)
            {
                if(status_arr[i] == 1)
                {
                    pthread_cancel(tid[i]);
                    status_arr[i] = 0;
                }
            }
            pthread_create(&tid[3], NULL, display4, NULL);
            status_arr[3] = 1;
        }
    }
}

void *recv_server_info(void *arg)
{
    //设置自己分离属性
    pthread_detach(pthread_self());

    int ret = -1;
    int socket_fd = *(int *)arg;
    char buf[1024];

    while(1)
    {
        memset(buf, 0, sizeof(buf));
        ret = recv(socket_fd, buf, sizeof(buf), 0);
        if (ret < 0)
        {
            printf("recv fail\n");
            close(socket_fd);
            return NULL;
        }
        else if(ret == 0)
        {
            printf("服务器掉线\n");
            close(socket_fd);
            return NULL;
        }
        else
        {
            //传输文件
            if(strcmp(TRANS, buf) == 0)
            {
                printf("准备接收文件\n");   
                recv_file(socket_fd);
            }

            //播放广告
            else if(strcmp(DISPL, buf) == 0)
            {
                printf("准备播放广告\n");
                disp_ad(socket_fd);
            }
        }
        printf("recv success ret:%d\n", ret);
    }  
    pthread_exit(NULL);      
}

void* tcp_client_ctl(void *arg)
{
    int ret = 0;

    //创建套接字(socket)
    //AF_INET:ipv4 SOCK_STREAM:tcp协议
    int socket_fd = socket(AF_INET,SOCK_STREAM,0);
    if(socket_fd < 0)
    {
        printf("socket fail\n");
        return NULL;
    }

    //填充服务器的ip地址和端口
    // struct sockaddr server_addr; //以前老的结构体
    struct sockaddr_in server_addr;//新的结构体(比就结构体好复制)
    server_addr.sin_family = AF_INET; //地址族
    server_addr.sin_port = htons(SERVER_PORT); //本机端口转换为网络端口host to net short
    server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);//将本机IP转换为网络ip

    //连接客户端(connect)
    // connect(socket_fd,服务器的地址和端口,地址的长度)
    ret = connect(socket_fd,(struct sockaddr *)&server_addr,sizeof(struct sockaddr_in));
    if(ret < 0)
    {
        printf("connect server fail\n");
        return NULL;
    }
    printf("连接服务器成功[%s][%d][%d]\n",SERVER_IP,SERVER_PORT,socket_fd);


    //创建一个子线层用来接受服务器的数据
    pthread_t tid;
    ret = pthread_create(&tid,NULL,recv_server_info,(void *)&socket_fd);
    if(ret != 0)
    {
        printf("pthread_create fail\n");
        return NULL;
    }
    pthread_join(tid, NULL);

    char buf[1024];
    while(1)
    {
        memset(buf,0,sizeof(buf));
        fgets(buf,sizeof(buf),stdin); //这个字符串可以带空格
        //给服务器发送数据(send/write)
        ret = send(socket_fd,buf,strlen(buf)-1,0); //-1是为了去掉回车符
        if(ret < 0)
        {
            printf("send fail\n");
            return NULL;
        }
        printf("send success ret:%d\n", ret);
    }

    //关闭套接字(close)
    close(socket_fd);
}